package sobchak;

import sobchak.tools.Room;
import sobchak.tools.CementPortion;
import sobchak.tools.Brick;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 13.03.2010
 * Time: 8:31:14
 * To change this template use File | Settings | File Templates.
 */
public class TajikThread extends Thread {
	public static final int BUILD_CEMENT_LIMIT = 20;
	private ITajik tajik;
	private TestNaciyanika beliy;
	private boolean active = false;


	public TajikThread(ITajik tajik, TestNaciyanika beliy) {
		this.tajik = tajik;
		this.beliy = beliy;
		active = tajik.toString() == NashaRashaTajik.DJUMSHUT;
	}

	@Override
	public void run() {
		while(!isInterrupted()) {
			if(active && beliy.getCementRest() <= BUILD_CEMENT_LIMIT)
				beliy.buildCementTank(tajik);

			CementPortion portion = beliy.cement_sync.getCementPortion(tajik);
			if(active && portion == null) {
				beliy.changeCementTank();
				continue;
			}

			Brick brick = tajik.getBrick(Room.getActiveRoom());

			try {
				tajik.putBrickOnWall(brick, portion);
			} catch (InterruptedException e) {
				e.printStackTrace();
				return;
			}


		}
	}
}
